// index.ts
// 获取应用实例
const app = getApp<IAppOption>()

Component({
  data: {
    imageList: ['pic_0.jpg', 'pic_1.jpg', 'pic_2.jpg', 'pic_3.jpg', 'pic_4.jpg', 'pic_5.jpg', 'pic_6.jpg', 'pic_7.jpg', 'pic_8.jpg'],
    variableList: [0, 1, 2, 3, 4, 5, 6, 7, 8],
    focusIndex: -1
  },
  methods: {
    onLoad() {

    },
    onClickIcon(e: any) {

      var focusIndex = this.data.focusIndex
      var variableList = this.data.variableList
      var index = e.currentTarget.dataset['index']
      if (focusIndex != -1) {
        if (focusIndex != index) {
          var tmp = variableList[focusIndex]
          variableList[focusIndex] = variableList[index]
          variableList[index] = tmp
          this.checkResult()
        }
        focusIndex = -1
      } else {
        focusIndex = index
      }
      this.setData({
        focusIndex: focusIndex,
        variableList: variableList
      })
    },
    startGame() {
      var variableList = this.shuffle(this.data.variableList)
      this.setData({
        variableList: variableList
      })
    },
    shuffle(arr: number[]) {
      return arr.sort(() => Math.random() - 0.5);
    },
    checkResult() {
      var variableList = this.data.variableList
      var checkResult = true
      for (var i = 0; i < variableList.length; i++) {
        if (variableList[i] != i) {
          checkResult = false
          break
        }
      }
      if (checkResult) {
        wx.showToast({
          title: '恭喜，拼好啦', //提示的内容
          duration: 2000, //持续的时间
          mask: true //显示透明蒙层 防止触摸穿透
        })
      }
    }
  },
})
